package com.example.druid;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class DruidExample {
    public static void main(String[] args) throws Exception {
        //3. 加载配置文件
        Properties properties = new Properties();
        InputStream in = DruidExample.class.getClassLoader().getResourceAsStream("druid.properties");
        properties.load(in);
        //创建DataSource对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        //获取连接
        Connection conn = dataSource.getConnection();
        String sql = "select * from user where username = ? and password =?";
        //获取执行SQL的对象
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setString(1,"zhangsan");
        preparedStatement.setString(2,"123");
        ResultSet rs = preparedStatement.executeQuery();
        // 判断登录是否成功
        if(rs.next()){
            System.out.println("登录成功~");
        }else{
            System.out.println("登录失败~");
        }

        //7. 释放资源
        rs.close();
        preparedStatement.close();
        conn.close();


    }
}
